<template>
  <div class="vip_dl">
    <div class="container">
      <div class="row">
        <div class="vip_dl_con">
          <div class="vip_dl_fr">
            <div class="vip_dl_fr2">
              <h1 class="hidden-xs">登录</h1>
              <ul class="welcome">
                欢迎来到IIFA大家庭。<br />请登录获取更多权限，IIFA助力您的职业生涯持续发展，培养世界思想领袖。
              </ul>
              <div class="denglu block">
                <div class="denglu1">
                  <el-form :rules="rules" :model="loginData" ref="ruleFormRef">
                    <div class="denglu1_con1 block">
                      <ul class="zhuce_ul">
                        <li class="li1">
                          <el-form-item prop="username">
                            <el-input
                              v-model="loginData.username"
                              placeholder="请输入登录用户名/邮箱/手机号码"
                            ></el-input>
                          </el-form-item>
                        </li>
                        <li class="li2">
                          <el-form-item prop="password">
                            <el-input
                              type="password"
                              v-model="loginData.password"
                              placeholder="请输入登录密码"
                              style="color: #a9a9a9"
                            ></el-input>
                          </el-form-item>
                        </li>

                        <li class="li3">
                          <el-form-item prop="captchaCode" style="width: 70%;">
                            <el-input
                              type="text"
                              v-model="loginData.captchaCode"
                              placeholder="请输入验证码"
                            >
                            
                          </el-input>
                          </el-form-item>
                          <img
                            title="点击刷新"
                            @click="captchaFunction"
                            style="cursor: pointer;"
                            :src="captchaData.captchaValue"
                          />
                        </li>

                        <li class="li4">
                          <div class="agreement">
                            <label>
                              <el-checkbox
                               :checked="checkedBool" 
                              /><span>同意</span>
                            </label>
                            <span class="tongyi"><i>《隐私协议》</i></span>
                          </div>
                          <router-link to="/forgotPsd">
                            <span class="span1">忘记密码?</span>
                          </router-link>
                        </li>
                        <li class="li5">
                          <el-button
                            type="primary"
                            :loading="submitLoading" 
                            @click.prevent="login(ruleFormRef)"
                          
                            class="fasong"
                            >立即登录</el-button
                          >
                        </li>
                      </ul>
                    </div>
                  </el-form>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ElMessage, FormRules, FormInstance } from "element-plus";
import { onMounted, ref, reactive } from "vue";
import { getCaptcha ,loginApi} from "@/api/login/login";
import type { CaptchaModel, LoginParam } from "@/api/login/login";
import store from "@/store";
import eventBus from '../event/mitt'
const router = useRouter()
const submitLoading = ref(false)
const checkedBool = ref(true);
const ruleFormRef = ref<FormInstance>();
const captchaData = ref<CaptchaModel>({ captchaKey: "", captchaValue: "" });
const rules = reactive<FormRules<LoginParam>>({
  username: [
    {
      required: true,
      message: "请输入登录用户名/邮箱/手机号码",
      trigger: "blur",
    },
  ],
  password: [{ required: true, message: "请输入登录密码", trigger: "blur" }],
  captchaCode: [{ required: true, message: "请输入验证码", trigger: "blur" }],
});

 

 
async function login(formEl: FormInstance | undefined) {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      submitLoading.value=true
      loginData.value.captchaKey=captchaData.value.captchaKey
      const response= await loginApi(loginData.value)
      submitLoading.value=false
      if(response.code===200 && response.data){
        ElMessage.success('登录成功')
        eventBus.emit('isLogin',true)
        store.commit('SET_TOKEN',response.data.tokenValue)
        router.push("/info");
      }else{
        loginData.value.captchaCode=''
        captchaFunction()
        ElMessage.error(response.msg) 
      }
    } else {
      console.log("error submit!", fields);
    }
  })
}

const loginData = ref<LoginParam>({
  username: "",
  password: "",
  captchaKey: '',
  captchaCode: "",
});
async function captchaFunction() {
  try {
    const captcha = await getCaptcha();
    if (captcha && captcha.data) {
      captchaData.value = captcha.data;
    }
  } catch (error) {
    console.error("Failed to fetch captcha:", error);
  }
}

onMounted(() => {
  captchaFunction();
});
</script>
<style scoped lang="less">
.vip_dl {
  width: 100%;
  background: url("../assets/img/1366-675.png") no-repeat center top;
  background-size: 100% 100%;
  height: 675px;
  display: flex;
  margin-top: -10px;
}
.vip_dl .container {
  width: 1200px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  align-content: center;
}
.vip_dl .container .row {
  display: flex;
  flex-direction: column;
  margin: 0;
}
.vip_dl_con {
  width: 800px;
}
.hidden-xs {
  color: #00499d;
  font-size: 32px;
  text-align: center;
  margin-bottom: 20px;
  font-weight: 800;
}
.el-input {
  height: 40px;
}
.vip_dl_fr {
  width: 60%;
  margin: 0 auto;
  background: #fff;
  padding: 25px 0;
  border-radius: 10px;
}
.vip_dl_fr2 {
  width: 80%;
  margin: 0 auto;
  padding: 10px 0 15px 0;
}
.vip_dl_fr2 > form > div.block {
  display: block;
}
.denglu1 > form > div {
  display: none;
}
.denglu1 > form > div.block {
  display: block;
}
.zhuce_ul li {
  width: 100%;
  margin-top: 15px;
}
.zhuce_ul li input {
  width: 100%;
  font-size: 14px;
  padding-left: 5%;
  border: 1px solid #e4e4e4;
  border-radius: 2px;
  height: 48px;
  line-height: 48px;
  background: #eff0f4 !important;
}
.li3 {
  position: relative;
}
.zhuce_ul li {
  width: 100%;
  margin-top: 15px;
}
.li3 .yzm {
  position: absolute;
  top: 0;
  right: 0;
  width: 40%;
  text-align: center;
  line-height: 45px;
  color: #6f727e;
  text-decoration: underline;
  font-size: 14px;
  background-color: Transparent;
  text-transform: none;
  outline: none;
  border: none;
}

.li5 .fasong {
  height: 50px;

  text-align: center;
  color: #fff;
  border-radius: 2px;
  background: #00499d;
  display: inline-block;
  width: 100%;
  font-size: 20px;
  border: 0;
  outline: 0;
}
.denglu1_con1 .li4 {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-direction: row;
  width: 100%;
  margin-top: 10px;
}
.denglu1_con1 .li4 .agreement {
  display: flex;
  flex-direction: row;
  align-items: center;
}
.denglu1_con1 .li4 .agreement label {
  margin: 0;
  display: flex;
  align-items: center;
  padding: 0;
}
.denglu1_con1 .li4 .agreement input {
  width: 13px;
  height: 13px;
  vertical-align: -2px;
  margin-right: 5px;
  float: left;
  padding-left: 0;
}
.denglu1_con1 .li4 span {
  font-size: 14px;
  display: inline-block;
  cursor: pointer;
  text-align: center;
  color: #6f727e;
}
.denglu1_con1 .li4 span i:hover {
  color: #007aff;
}
.agreement span i {
  font-style: normal;
  color: #007aff;
}
.zhuce_ul li.li3 {
  display: flex;
  line-height: 48px;
}
.zhuce_ul li.li3 input {
  width: 60%;
}
.zhuce_ul li.li3 img {
  height: 40px;
  margin-left: 15px;
}
.vip_dl_fr2 .welcome {
  font-family: "Microsoft Yahei", Tahoma, Helvetica, Arial, sans-serif;
  font-size: 16px;
  background: #fff;
  color: #333;
  border-radius: 2px;
  padding: 0 0 5px 0;
  text-align: left;
}
</style>
../event/mitt